package com.yw.atp.action;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.yw.atp.model.Command;
import com.yw.atp.model.Reply;
import com.yw.atp.model.ReplyBean;
import com.yw.atp.model.Staff;
import com.yw.atp.service.PersistenceService;
import com.yw.atp.service.ReplyService;
import com.yw.atp.service.StaffService;
import com.yw.atp.util.BeanUtil;
import com.yw.atp.util.Constants;
import com.yw.atp.util.DateUtil;
import com.yw.atp.util.StringUtil;
import com.yw.atp.web.ActionBase;

@Scope("prototype")
@Component("replyAction")
public class ReplyAction extends ActionBase implements ServletRequestAware{
	private static final long serialVersionUID = 1L;
	@Autowired
	private PersistenceService service;
	@Autowired
    private ReplyService replyService;
	@Autowired
	private StaffService staffService;
	HttpServletRequest request;
	public List<Reply> replyList;
	public Command command;
	public Staff staff;
	public Reply reply;
	public String replyIds;
	public String res;
	public int start = 0;
	public int limit = 20;
	public String serverID;
	public String mobileID;
	public String mID;
	public String replyBeans;

	public String list(){
		if(command==null&&staff==null){
			replyList = service.findAll(Reply.class,0);
		}else{
			if(reply==null){
				reply = new Reply();
			}
			if(reply.getOwner()==null){
				reply.setOwner(staff);
			}
			System.out.println(String.valueOf(command));
			if(command!=null&&StringUtil.checkID(command.getId())){
				Command comm = service.find(Command.class,command.getId());
				System.out.println(String.valueOf(comm));
				reply.setCommand(comm);
			}
			replyList = replyService.getReplys(reply);
		}
		JSONObject js = new JSONObject();
		try {
			if(replyList!=null&&replyList.size()>0){
				js.put("totalCount",replyList.size());
				int len = replyList.size();
				start = start>(len-1)?(len-1):start;
				limit = limit>(len-start)?(len):(start+limit);
				js.put("topics",Reply.capture(replyList.subList(start,limit)));
			}
			res = js.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String add(){
		if(staff!=null&&StringUtil.isNotNull(staff.getMobileID())){
			Staff owner = staffService.findByMobile(staff.getMobileID());
			System.out.println(owner!=null?"---owner : "+owner.getName():"owner is null");
			reply.setOwner(owner);
		}else{
			System.out.println("---owner :  owner is null");
		}
		if(reply!=null&&command!=null&&StringUtil.checkID(command.getId())&&StringUtil.isNotNull(reply.getContent())&&reply.getOwner()!=null&&StringUtil.checkID(reply.getOwner().getId())){
			command = service.load(command);
			if(reply.getCommand()==null){reply.setCommand(command);}
			if(reply.getId()==null||reply.getId().intValue()<1){
				if(reply.getCreatedAt()==null){reply.setCreatedAt(new Date());};
			}
			service.save(reply);
			return SUCCESS;
		}else{
			return INPUT;
		}
	}
	
	public String del(){
		if(replyIds!=null&&!"".equals(replyIds)){
			service.remove(Reply.class.getName(),replyIds);
		}
		return SUCCESS;
	}
	
	public String hasReply(){
		Staff staff = staffService.findByMobile(mobileID);
		System.out.println("-------- 开始判断"+(staff!=null?staff.getName():"nobody")+" 的第[ "+serverID+" ]号信息的回复是否存在-------");
		if(staff!=null&&StringUtil.checkID(staff.getId())){
			Command c = service.find(Command.class,Integer.valueOf(serverID));
			System.out.println("--------command : "+String.valueOf(c)+"------commType : "+(c!=null?c.getCommType():"null"));
			if(c!=null&&StringUtil.checkID(c.getId())){
				Reply r = replyService.getTheReplysOfStaff(c,staff);
				if(r!=null&&StringUtil.checkID(r.getId())){
					mID = String.valueOf(c.getId());
					System.out.println("-------- "+(staff!=null?staff.getName():"nobody")+" 的第[ "+serverID+" ]号信息的回复存在-------");
					return SUCCESS;
				}
			}
		}
		System.out.println("-------- "+(staff!=null?staff.getName():"nobody")+" 的第[ "+serverID+" ]号信息的回复不存在-------");
		return INPUT;
	}
	
	public String load(){
		try{
			Staff staff = staffService.findByMobile(mobileID);
			System.out.println("--------"+(staff!=null?staff.getName():"nobody")+" 开始加载第[ "+serverID+" ]号信息的回复-------");
			if(staff!=null&&staff.getId()!=null&&staff.getId().intValue()>0&&StringUtil.isNotNull(serverID)){
				Command c = service.find(Command.class,Integer.valueOf(serverID));
				System.out.println("--------command : "+String.valueOf(c)+"------commType : "+(c!=null?c.getCommType():"null"));
				if(c!=null&&StringUtil.checkID(c.getId())){
					JSONArray ja = new JSONArray();
					if("2".equals(c.getCommType())){
						String cont = "";
						String rt  = c.getUpdateAt()==null?" ":DateUtil.getClientFormatA(c.getUpdateAt());
						if(Constants.COMMAND_STATUS_APCL.equals(c.getStatus())){
							cont = "已安排处理";
						}else if(Constants.COMMAND_STATUS_YCL.equals(c.getStatus())){
							cont = "已处理完毕";
						}else if(Constants.COMMAND_STATUS_YZF.equals(c.getStatus())){
							cont = "已转发相关人员";
						}else if(Constants.COMMAND_STATUS_DEL.equals(c.getStatus())){
							cont = "已删除";
						}else{
							System.out.println("commType : 2 , status : other");
							return INPUT;
						} 
						ja.put("0;;"+cont+";;"+rt);
					}else{
						List<Reply>  relys = replyService.loadReplys(c.getId(),staff);
						if(relys!=null&&relys.size()>0){
							System.out.println("------------relys size : "+relys.size());
							for (Reply r:relys){
								ja.put(BeanUtil.formatReply(r));
							}
						}else{
							System.out.println("------------relys size : 0");
							return INPUT;
						}
					}
					JSONObject jo = new JSONObject();
					jo.put("failure","false");
					jo.put("success","true");
					jo.put("id",mID);
					jo.put("res",ja);
					res = jo.toString();
					return SUCCESS;
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return INPUT;
	}
	
	public String updateReply(){
		try{
			Staff staff = staffService.findByMobile(mobileID);
			System.out.println("--------"+(staff!=null?staff.getName():"nobody")+" 开始同步回复.");
			if(staff!=null&&StringUtil.checkID(staff.getId())){
				List<ReplyBean> rbl = BeanUtil.parReply(replyBeans);
				mID = null;
				for(ReplyBean rb:rbl){
					try{
						Command c = service.find(Command.class,Integer.valueOf(rb.servID));
						if(c!=null&&StringUtil.checkID(c.getId())){
							Reply reply = replyService.getTheReplysOfStaff(c,staff);
							if(reply==null||StringUtil.checkID(reply.getId())){
								reply = new Reply();
								reply.setCommand(c);
								reply.setOwner(staff);
								reply.setContent(rb.content);
								reply.setUpdateAt(DateUtil.parClientData(rb.createdAt));
								reply.setCreatedAt(DateUtil.parClientData(rb.createdAt));
								service.save(reply);
							}
						}
						mID = mID!=null?(mID+","+rb.servID):rb.servID;
					}catch(Exception ex){
						System.out.println("command id :"+rb.servID+" is not exsist");
					}
				}
				return SUCCESS;
			}
		}catch(Exception e){
			return INPUT;
		}
		return INPUT;
	}
	
	public void setServletRequest(HttpServletRequest request) {
        if(request==null){
            request = ServletActionContext.getRequest();
        }
        this.request = request;
    }
	
	public PersistenceService getService() {
		return service;
	}
	
	public void setService(PersistenceService service) {
		this.service = service;
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}
	
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public Staff getStaff() {
		return staff;
	}

	public void setStaff(Staff staff) {
		this.staff = staff;
	}
	public List<Reply> getReplyList() {
		return replyList;
	}

	public void setReplyList(List<Reply> replyList) {
		this.replyList = replyList;
	}

	public Command getCommand() {
		return command;
	}

	public void setCommand(Command command) {
		this.command = command;
	}

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}

	public ReplyService getReplyService() {
		return replyService;
	}

	public void setReplyService(ReplyService replyService) {
		this.replyService = replyService;
	}
	public String getRes() {
		return res;
	}

	public void setRes(String res) {
		this.res = res;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
    
	public StaffService getStaffService() {
		return staffService;
	}

	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}
	
	public String getReplyIds() {
		return replyIds;
	}

	public void setReplyIds(String replyIds) {
		this.replyIds = replyIds;
	}
	
	public String getServerID() {
		return serverID;
	}

	public void setServerID(String serverID) {
		this.serverID = serverID;
	}

	public String getMobileID() {
		return mobileID;
	}

	public void setMobileID(String mobileID) {
		this.mobileID = mobileID;
	}

	public String getmID() {
		return mID;
	}

	public void setmID(String mID) {
		this.mID = mID;
	}

	public String getReplyBeans() {
		return replyBeans;
	}

	public void setReplyBeans(String replyBeans) {
		this.replyBeans = replyBeans;
	}

}
